<template>
	<ls-page :hidden="true" :hideTopbar="true" backgroundImage="https://oss.jtmckj.com/wmp/kbq/i_back1.png"
		:default-bg="false">

	</ls-page>
</template>

<script setup>
	import { onLoad, onShow } from '@dcloudio/uni-app';
	import { HFiveLogin, HFiveRegister, GetBaseConfigInfoH5,HFiveAppObjProcess } from '@/api/public.js'
	
	// 扫码二维码获取brandCode orgCode并存储起来
	onLoad((options) => {
		console.log('onLoad...')
		getWxUserInfoCode()
	});
	onShow(() => {
		//  
	});  

	// 后端获取
	const getWxInFo = async (code) => {
		// const {data }
		let modal = {
			"brandCode": "0004", //   uni.setStorageSync
			"orgCode": "0017", //   uni.setStorageSync
			"code": code
		}
		const data = await  HFiveLogin(modal)
		console.log('data:', data)
		uni.setStorageSync("openId", data.openId)
		uni.setStorageSync("unionID", data.unionID)
        uni.setStorageSync("token", data.userInfo.token)

		// 已注册
		if (data.loginstatus === 1) { 
			let modal1 = { 
				"brandCode": "0004",
				"orgCode": "0017"
			} 
			// 直接登录
			const user  = await GetBaseConfigInfoH5(modal1) 
			console.log('data2：',user) 
			uni.setStorageSync('userInfo',user)
			uni.navigateTo({
				url: '/pages/home/index'
			})
		} else {
			// 去开卡  -  开卡成功需要跳转到- 我的页面  
			// post 
			let modal2 = {
				"functionName": "CreadMemberCard",
				"brandCode": "0004",
				"orgCode": "0017",
				"contentData": ""
			}
			const datauUrl = await HFiveAppObjProcess(modal2)  
			// window.location.href = datauUrl
			// https://jtmckj.cn:4080/?card_id=pDCMP6-hP2eN9QW26o7l7laz0238&encrypt_code=TN%2FXy6aurFz8r7BauFz3ijV7qx0s3836f3NXyGuru2g%3D&openid=oDCMP6ytbfkBhWXk5o5LUUcLtE48&outer_str=0017&activate_ticket=fDZv9eMQAFfrNr3XBoqhb60OzCeHzNF3OMl%2FaVrovyMErjveAFKWU5qKhw4i2Sacb8dCcZSl7OSyai4h2vAYNnCreFQAnBGE3XQ8bgMNrwQ%3D#/login
			// activate_ticket=fDZv9eMQAFfrNr3XBoqhb60OzCeHzNF3OMl%2FaVrovyMErjveAFKWU5qKhw4i2Sacb8dCcZSl7OSyai4h2vAYNnCreFQAnBGE3XQ8bgMNrwQ%3D#/login
			// 后端提交表单调整到我的页面 - 然后token替换掉,然后去获取用户信息  
			
			// 注册接口 - 正常在我的页面
			// const urlObj = new URL(longUrl);
			// const activateTicket = urlObj.searchParams.get('activate_ticket');
			let modalRegister = {
			  "activateTicket": "fDZv9eMQAFfrNr3XBoqhb60OzCeHzNF3OMl%2FaVrovyMErjveAFKWU5qKhw4i2Sacb8dCcZSl7OSyai4h2vAYNnCreFQAnBGE3XQ8bgMNrwQ%3D",
			  "openid": uni.getStorageSync("openId"),
			  "unionID":uni.getStorageSync("unionID"),
			  "brandCode": "0004",
			  "orgCode": "0017"
			}
			const dataRegister = await HFiveRegister(modalRegister) 
			console.log('dataRegister：',dataRegister) 
			let modaluser = {
				"brandCode": "0004",
				"orgCode": "0017"
			} 
			const user  = await GetBaseConfigInfoH5(modaluser)
			console.log('user：',user) 
			uni.setStorageSync('userInfo',user)
			setTimeout(function() {
				uni.navigateTo({
				    url: '/pages/home/index'
				})
			}, 1000); 
		} 
	 
		//  
		// get
		// orgCode  brandCode

		// setTimeout(() => {
		// 	uni.navigateTo({
		// 		url: '/pages/home/index'
		// 	})
		// }, 1000) 
	}



	const getWxUserInfoCode = () => {
		console.log('getWxUserInfoCode...')
		//获取当前页面的url
		let link = window.location.href;
		console.log(link, 'link');
		let code = null;
		let appid = 'wxd536727fd1a498a7';
		let uri = 'http://172.18.3.62:5173/kbq'; //重定向地址 'https://jtmckj.cn/kbq
		// let appsecret = "填写公众号开发者秘钥"; //公众号开发者秘钥  需要前端自行获取时才需要
		// 判断link中有没有code=字符串，
		if (link.indexOf('code=') === -1) {
			let uri2 = encodeURIComponent(uri)
			window.location.href =
				`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri2}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
		} else {
			let temp = link.split("code=")[1];
			code = temp.split("&")[0]; 
			getWxInFo(code)
			// getWxInFoByQD(code) 
		}
	}


	// 前端通过微信api自行获取openid
	const getWxInFoByQD = () => {
		console.log('getWxInFoByQD...')
		uni.request({
			url: "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + appsecret +
				"&code=" + code + "&grant_type=authorization_code",
			method: "GET",
			success: function(res) {
				console.log(res)
				if (res.statusCode === 200) {
					//以获取的openid
					console.log(res.data.openid)
					this.$api.msg('用户信息openid' + res.data.openid);
				}
			},
			fail: function(err) {
				console.log(err)
			}
		})
	}
	
	// https://jtmckj.cn:4080/?card_id=pDCMP6-hP2eN9QW26o7l7laz0238&encrypt_code=TN%2FXy6aurFz8r7BauFz3ijV7qx0s3836f3NXyGuru2g%3D&openid=oDCMP6ytbfkBhWXk5o5LUUcLtE48&outer_str=0017&activate_ticket=fDZv9eMQAFfrNr3XBoqhb60OzCeHzNF3OMl%2FaVrovyMErjveAFKWU5qKhw4i2Sacb8dCcZSl7OSyai4h2vAYNnCreFQAnBGE3XQ8bgMNrwQ%3D#/login
	// activate_ticket=fDZv9eMQAFfrNr3XBoqhb60OzCeHzNF3OMl%2FaVrovyMErjveAFKWU5qKhw4i2Sacb8dCcZSl7OSyai4h2vAYNnCreFQAnBGE3XQ8bgMNrwQ%3D#/login
	// 后端提交表单调整到我的页面 - 然后token替换掉,然后去获取用户信息  
	
	// 注册接口 - 正常在我的页面
	// const urlObj = new URL(longUrl);
	// const activateTicket = urlObj.searchParams.get('activate_ticket');
	// let modalRegister = {
	//   "activateTicket": "fDZv9eMQAFfrNr3XBoqhb60OzCeHzNF3OMl%2FaVrovyMErjveAFKWU5qKhw4i2Sacb8dCcZSl7OSyai4h2vAYNnCreFQAnBGE3XQ8bgMNrwQ%3D",
	//   "openid": uni.getStorageSync("openId"),
	//   "unionID":uni.getStorageSync("unionID"),
	//   "brandCode": "0004",
	//   "orgCode": "0017"
	// }
	// const dataRegister = await HFiveRegister(modalRegister) 
	// console.log('dataRegister：',dataRegister) 
	// let modaluser = {
	// 	"brandCode": "0004",
	// 	"orgCode": "0017"
	// } 
	// const user  = await GetBaseConfigInfoH5(modaluser)
	// console.log('user：',user) 
	// uni.setStorageSync('userInfo',user)
	// setTimeout(function() {
	// 	uni.navigateTo({
	// 	    url: '/pages/home/index'
	// 	})
	// }, 1000); 
	
	// const initUserInfo = () => {
	// 	let modal = { }
	// 	goodsPublic(modal).then(res => { 
	// 		if (res.opFlag !== '1') { return }
	// 		let result = res.content.rowData       
	// 		userModal.value = result.find(item => item.cardType === 2)  // 类型2 门店卡（当前只做门店卡不做品牌卡）   
	// 		store.SetUserInfo(userModal.value || '')   // 存储用户基础信息 
	// 	})
	// }; 
	// const initType = async () => { 
	// 	let modal = { } 
	// 	const { opFlag, content } = await goodsPublic(modal) 
	// }; 
</script>

<style scoped lang="less">

</style>